package com.appspot.didit.server;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.*;

public class CSVFile extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2767939555947229044L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			String variable = request.getParameter("var");
			HttpSession session = request.getSession();
			String strout = (String) session.getAttribute(variable);
        	String fileName = "Export.csv";
        	response.reset();
        	response.setContentType( "application/vnd.csv"); 
            //set header as attachment
        	response.setHeader("Content-Disposition", "attachment; filename=" + fileName); 
        	PrintWriter out;
    		out = response.getWriter();
    		out.write(strout);
        }
        catch (Exception e) {}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException {
        try {
			String variable = request.getParameter("var");
			HttpSession session = request.getSession();
			String strout = (String) session.getAttribute(variable);
        	String fileName = "Export.csv";
        	response.reset();
        	response.setContentType( "application/vnd.csv"); 
            //set header as attachment
        	response.setHeader("Content-Disposition", "attachment; filename=" + fileName); 
        	PrintWriter out;
    		out = response.getWriter();
    		out.write(strout);
        }
        catch (Exception e) {}
	}
}